package com.omicrondev.flightops;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;

public class EditAcType extends Activity {
    FlightOps mApp;
    AcType mAcType;
    EditText bewBox, beaBox, toLimitBox, ldgLimitBox, zfLimitBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_ac_type);

        mApp = (FlightOps)getApplicationContext();

        mAcType = mApp.mAcType;
        //mAcType = new AcType();

        // General Info

        bewBox = (EditText)findViewById(R.id.bew);
        beaBox = (EditText)findViewById(R.id.bea);
        toLimitBox = (EditText)findViewById(R.id.toLimit);
        ldgLimitBox = (EditText)findViewById(R.id.ldgLimit);
        zfLimitBox = (EditText)findViewById(R.id.zfLimit);

        bewBox.setText(((Integer)mApp.mAcType.mBEW).toString());
        DecimalFormat df = new DecimalFormat("#.##");
        beaBox.setText(df.format(mApp.mAcType.mBEA));
        toLimitBox.setText(((Integer)mApp.mAcType.mToLimit).toString());
        ldgLimitBox.setText(((Integer)mApp.mAcType.mLdgLimit).toString());
        zfLimitBox.setText(((Integer)mApp.mAcType.mZfLimit).toString());

        bewBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!hasFocus) {
                    BigDecimal value;
                    try {
                        value = new BigDecimal(((EditText) view).getText().toString());
                    } catch (NumberFormatException e) {
                        value = BigDecimal.ZERO;
                    }

                    mApp.mAcType.mBEW = value.intValue();
                }
            }
        });

        beaBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!hasFocus) {
                    BigDecimal value;
                    try {
                        value = new BigDecimal(((EditText) view).getText().toString());
                    } catch (NumberFormatException e) {
                        value = BigDecimal.ZERO;
                    }

                    mApp.mAcType.mBEA = value.doubleValue();
                }
            }
        });

        toLimitBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!hasFocus) {
                    BigDecimal value;
                    try {
                        value = new BigDecimal(((EditText) view).getText().toString());
                    } catch (NumberFormatException e) {
                        value = BigDecimal.ZERO;
                    }

                    mApp.mAcType.mToLimit = value.intValue();
                }
            }
        });

        ldgLimitBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!hasFocus) {
                    BigDecimal value;
                    try {
                        value = new BigDecimal(((EditText) view).getText().toString());
                    } catch (NumberFormatException e) {
                        value = BigDecimal.ZERO;
                    }

                    mApp.mAcType.mLdgLimit = value.intValue();
                }
            }
        });

        zfLimitBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!hasFocus) {
                    BigDecimal value;
                    try {
                        value = new BigDecimal(((EditText) view).getText().toString());
                    } catch (NumberFormatException e) {
                        value = BigDecimal.ZERO;
                    }

                    mApp.mAcType.mZfLimit = value.intValue();
                }
            }
        });

        // CG Stations
        //addStationItems();

        Button button = (Button)findViewById(R.id.addStation);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mApp.mAcType.mStations.add(new CGStation("Empty", 0, 0, 0, false, false));

                Intent intent = new Intent(view.getContext(), EditCgStation.class);
                intent.putExtra("station", (mApp.mAcType.mStations.size() - 1));
                startActivity(intent);
            }
        });

        // Forward CG Limits
        //addLimitItems(mAcType.mFwdCG, (LinearLayout)findViewById(R.id.fwdLimits));
        button = (Button)findViewById(R.id.addFwd);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mApp.mAcType.mFwdCG.add(new CGLimit(0, 0));

                LinearLayout limitGroup = (LinearLayout)findViewById(R.id.fwdLimits);
                clearLimitItems(limitGroup);
                addLimitItems(mAcType.mFwdCG, limitGroup);
            }
        });

        // Aft CG Limits
        //addLimitItems(mAcType.mAftCG, (LinearLayout)findViewById(R.id.aftLimits));
        button = (Button)findViewById(R.id.addAft);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mApp.mAcType.mAftCG.add(new CGLimit(0, 0));

                LinearLayout limitGroup = (LinearLayout)findViewById(R.id.aftLimits);
                clearLimitItems(limitGroup);
                addLimitItems(mAcType.mAftCG, limitGroup);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();

        mApp.saveData();
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        clearStationItems();
        addStationItems();
    }

    @Override
    protected void onResume() {
        super.onRestart();

        clearStationItems();
        addStationItems();

        clearLimitItems((LinearLayout)findViewById(R.id.fwdLimits));
        addLimitItems(mAcType.mFwdCG, (LinearLayout)findViewById(R.id.fwdLimits));

        clearLimitItems((LinearLayout)findViewById(R.id.aftLimits));
        addLimitItems(mAcType.mAftCG, (LinearLayout)findViewById(R.id.aftLimits));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.edit_ac_type, menu);
        return true;
    }

    public void addStationItems() {
        Collections.sort(mApp.mAcType.mStations);

        LinearLayout inputGroup = (LinearLayout)findViewById(R.id.stations);

        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        ListIterator<CGStation> itr = mAcType.mStations.listIterator();
        CGStation station;
        while (itr.hasNext()) {
            station = itr.next();

            View view = inflater.inflate(R.layout.edit_station, null);

            TextView name = (TextView)view.findViewById(R.id.name);
            name.setText(station.mName);

            TextView arm = (TextView)(view.findViewById(R.id.arm));
            arm.setText(String.valueOf(station.mArm));

            view.setTag((Integer)itr.previousIndex());

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(view.getContext(), EditCgStation.class);
                    intent.putExtra("station", (Integer)(view.getTag()));
                    startActivity(intent);
                }
            });

            Button button = (Button)(view.findViewById(R.id.remove));
            button.setTag((Integer)itr.previousIndex());
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clearStationItems();
                    mApp.mAcType.mStations.remove(((Integer)(view.getTag())).intValue());
                    addStationItems();
                }
            });

            inputGroup.addView(view);
        }
    }

    public void clearStationItems() {
        LinearLayout inputGroup = (LinearLayout)findViewById(R.id.stations);

        inputGroup.removeAllViews();
    }

    public void addLimitItems(ArrayList<CGLimit> limits, LinearLayout limitGroup) {
        Collections.sort(limits);

        ListIterator<CGLimit> cgItr = limits.listIterator();

        CGLimit cgLimit;
        while (cgItr.hasNext()) {
            cgLimit = cgItr.next();

            LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.edit_limit, null);

            EditText weightBox = (EditText)(view.findViewById(R.id.weight));
            weightBox.setText(String.valueOf(cgLimit.mWeight));
            weightBox.setTag(cgLimit);

            weightBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    if (!hasFocus) {
                        EditText input = (EditText)view;
                        BigDecimal value;
                        try {
                            value = new BigDecimal(input.getText().toString());
                        } catch (NumberFormatException e) {
                            value = BigDecimal.ZERO;
                        }

                        CGLimit limit = (CGLimit)(view.getTag());
                        limit.mWeight = value.intValue();

                        //updateDisplay();
                    }
                }
            });

            EditText armBox = (EditText)(view.findViewById(R.id.arm));
            DecimalFormat df = new DecimalFormat("#.##");
            armBox.setText(df.format(cgLimit.mArm));
            armBox.setTag(cgLimit);

            armBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    if (!hasFocus) {
                        EditText input = (EditText)view;
                        BigDecimal value;
                        try {
                            value = new BigDecimal(input.getText().toString());
                        } catch (NumberFormatException e) {
                            value = BigDecimal.ZERO;
                        }

                        CGLimit limit = (CGLimit)(view.getTag());
                        limit.mArm = value.floatValue();

                        //updateDisplay();
                    }
                }
            });

            Button button = (Button)(view.findViewById(R.id.remove));
            button.setTag(cgLimit);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CGLimit cgLimit = (CGLimit)view.getTag();
                    LinearLayout limitGroup;
                    ArrayList<CGLimit> limits;

                    if (mApp.mAcType.mFwdCG.contains(cgLimit)) {
                        limitGroup = (LinearLayout)findViewById(R.id.fwdLimits);
                        limits = mApp.mAcType.mFwdCG;
                    }
                    else if (mApp.mAcType.mAftCG.contains(cgLimit)) {
                        limitGroup = (LinearLayout)findViewById(R.id.aftLimits);
                        limits = mApp.mAcType.mAftCG;
                    }
                    else {
                        return;
                    }

                    clearLimitItems(limitGroup);
                    limits.remove(cgLimit);
                    addLimitItems(limits, limitGroup);
                }
            });

            limitGroup.addView(view);
        }
    }

    public void clearLimitItems(LinearLayout limitGroup) {
        limitGroup.removeAllViews();
    }
}
